package frontend;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
//import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class BuyProperty extends JDialog {


	//VARIABLES
	private static final long serialVersionUID = 1L;
	private String image;
	private BoardFrame boardFrame;
	private JButton auctionButton;
	private JButton buyButton;
	private JLabel backgroundImage;
	private JLabel titleDeed;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
	
	
	//CONSTRUCTORS
	public BuyProperty() {
		initComponents();
	}
	public BuyProperty(BoardFrame parent, String title, String image) {
		super(parent, title, true);
		this.image = image;
		this.boardFrame = parent;
		initComponents();
	}


	//INITIALIZE COMPONENTS
	private void initComponents() {
		//setUndecorated(true);
		//getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); 
		
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
		    public void windowClosing(WindowEvent we) {
		    }
		});
		
		setLocationRelativeTo(this.boardFrame);
		setLocation(250,175);
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(Color.white);
		setResizable(false);
		setForeground(Color.black);
		setLayout(new GroupLayout());
		add(initBuyButton(), new Constraints(new Leading(92, 80, 6, 6), new Leading(406, 10, 10)));
		add(initAuctionButton(), new Constraints(new Leading(180, 80, 10, 10), new Leading(406, 6, 6)));
		add(initTitleDeed(), new Constraints(new Leading(0, 6, 6), new Leading(0, 6, 6)));
		add(initBackgroundImage(), new Constraints(new Leading(0, 6, 6), new Leading(0, 6, 6)));
		setSize(269, 467);
	}
	//GUI Component Definitions
	private JButton initBuyButton() {
		if (buyButton == null) {
			buyButton = new JButton();
			buyButton.setText("BUY");
			buyButton.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent event) {
					buyButtonActionActionPerformed(event);
				}
			});
		}
		return buyButton;
	}
	private JButton initAuctionButton() {
		if (this.auctionButton == null) {
			this.auctionButton = new JButton();
			this.auctionButton.setText("Auction");
			if(this.boardFrame.getPlayerNum()<=1)
				this.initAuctionButton().setEnabled(false);
			this.auctionButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					auctionButtonActionActionPerformed(event);
				}
			});
		}
		return this.auctionButton;
	}
	private JLabel initTitleDeed() {
		if (this.titleDeed == null) {
			this.titleDeed = new JLabel();
			this.titleDeed.setIcon(new ImageIcon(getClass().getResource("/images/deedsTitle/" + this.image)));
		}
		return this.titleDeed;
	}
	private JLabel initBackgroundImage() {
		if (backgroundImage == null) {
			backgroundImage = new JLabel();
			backgroundImage.setIcon(new ImageIcon(getClass().getResource("/images/background/Property.jpg")));
		}
		return backgroundImage;
	}
	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}


	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				BuyProperty dialog = new BuyProperty();
				dialog.setDefaultCloseOperation(BuyProperty.EXIT_ON_CLOSE);
				dialog.setTitle("Buy Property?");
				dialog.setLocationRelativeTo(null);
				dialog.getContentPane().setPreferredSize(dialog.getSize());
				dialog.pack();
				dialog.setVisible(true);
			}
		});
	}


	//METHODS
	//Setup Property Ownership
	public boolean setupOwnership() {
		boolean transactionDone = false;
		int position = this.boardFrame.getCurrentPlayer().getToken().getPosition();
		
		if( 12==position || 28==position ) {//SETUP UTILITY OWNERSHIP!!!
				this.boardFrame.getBoard().getContainer(this.boardFrame.getCurrentPlayer().getToken().getPosition()).getUtility().setPropertyOwner(this.boardFrame.getCurrentPlayer());
				this.boardFrame.getCurrentPlayer().setCash(this.boardFrame.getCurrentPlayer().getCash() - this.boardFrame.getBoard().getContainer(this.boardFrame.getCurrentPlayer().getToken().getPosition()).getUtility().getPropertyPrice());
				transactionDone = true;
					
				if(12==this.boardFrame.getCurrentPlayer().getToken().getPosition())
					this.boardFrame.getCurrentPlayer().acquireDeed(22);
				if(28==this.boardFrame.getCurrentPlayer().getToken().getPosition())
					this.boardFrame.getCurrentPlayer().acquireDeed(23);

				if(null!=this.boardFrame.getCurrentPlayer().getDeed(22)&&null!=this.boardFrame.getCurrentPlayer().getDeed(23)) {
					this.boardFrame.getBoard().getContainer(12).getUtility().setMonopoly(true);
					this.boardFrame.getBoard().getContainer(28).getUtility().setMonopoly(true);
				}
		}
		else {
			if( 5==position || 15==position || 25==position || 35==position ) {
					this.boardFrame.getBoard().getContainer(this.boardFrame.getCurrentPlayer().getToken().getPosition()).getStation().setPropertyOwner(this.boardFrame.getCurrentPlayer());
					this.boardFrame.getCurrentPlayer().setCash(this.boardFrame.getCurrentPlayer().getCash() - this.boardFrame.getBoard().getContainer(this.boardFrame.getCurrentPlayer().getToken().getPosition()).getStation().getPropertyPrice());
					transactionDone = true;
					if(5==this.boardFrame.getCurrentPlayer().getToken().getPosition())
						this.boardFrame.getCurrentPlayer().acquireDeed(24);
					if(15==this.boardFrame.getCurrentPlayer().getToken().getPosition())
						this.boardFrame.getCurrentPlayer().acquireDeed(25);
					if(25==this.boardFrame.getCurrentPlayer().getToken().getPosition())
						this.boardFrame.getCurrentPlayer().acquireDeed(26);
					if(35==this.boardFrame.getCurrentPlayer().getToken().getPosition())
						this.boardFrame.getCurrentPlayer().acquireDeed(27);
			}
			else {
					this.boardFrame.getBoard().getContainer(this.boardFrame.getCurrentPlayer().getToken().getPosition()).getEstate().setPropertyOwner(this.boardFrame.getCurrentPlayer());
					this.boardFrame.getCurrentPlayer().setCash(this.boardFrame.getCurrentPlayer().getCash() - this.boardFrame.getBoard().getContainer(this.boardFrame.getCurrentPlayer().getToken().getPosition()).getEstate().getPropertyPrice());
					transactionDone = true;
					
					if( 1==position || 3==position ) {
						if(1==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(0);
						if(3==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(1);
						
						if(null!=this.boardFrame.getCurrentPlayer().getDeed(0) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(1)) {
								this.boardFrame.getBoard().getContainer(1).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(1).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(3).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(3).getEstate().setBuildable(true);
								/*
								System.out.println(this.boardFrame.getBoard().board[1].getEstate().getMonopoly());
								System.out.println(this.boardFrame.getBoard().board[3].getEstate().getMonopoly());
								*/
						}
					}
					
					if( 6==position || 8==position || 9==position) {
						if(6==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(2);
						if(8==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(3);
						if(9==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(4);
						
						if(null!=this.boardFrame.getCurrentPlayer().getDeed(2) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(3) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(4)) {
								this.boardFrame.getBoard().getContainer(6).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(6).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(8).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(8).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(9).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(9).getEstate().setBuildable(true);
								/*
								System.out.println(this.boardFrame.getBoard().board[6].getEstate().getMonopoly());
								System.out.println(this.boardFrame.getBoard().board[8].getEstate().getMonopoly());
								System.out.println(this.boardFrame.getBoard().board[9].getEstate().getMonopoly());
								*/
						}
					}
					
					if( 11==position || 13==position || 14==position) {
						if(11==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(5);
						if(13==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(6);
						if(14==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(7);
						
						if(null!=this.boardFrame.getCurrentPlayer().getDeed(5) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(6) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(7)) {
								this.boardFrame.getBoard().getContainer(11).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(11).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(13).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(13).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(14).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(14).getEstate().setBuildable(true);
								/*
								System.out.println(this.boardFrame.getBoard().board[11].getEstate().getMonopoly());
								System.out.println(this.boardFrame.getBoard().board[13].getEstate().getMonopoly());
								System.out.println(this.boardFrame.getBoard().board[14].getEstate().getMonopoly());
								*/
						}
					}
					
					if( 16==position || 18==position || 19==position) {
						if(16==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(8);
						if(18==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(9);
						if(19==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(10);
						
						if(null!=this.boardFrame.getCurrentPlayer().getDeed(8) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(9) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(10)) {
								this.boardFrame.getBoard().getContainer(16).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(16).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(18).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(18).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(19).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(19).getEstate().setBuildable(true);
						}
					}
					
					if( 21==position || 23==position || 24==position ) {
						if(21==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(11);
						if(23==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(12);
						if(24==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(13);
						
						if(null!=this.boardFrame.getCurrentPlayer().getDeed(11) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(12) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(13)) {
								this.boardFrame.getBoard().getContainer(21).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(21).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(23).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(23).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(24).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(24).getEstate().setBuildable(true);
						}
					}
					
					if( 26==position || 27==position || 29==position ) {
						if(26==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(14);
						if(27==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(15);
						if(29==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(16);
						
						if(null!=this.boardFrame.getCurrentPlayer().getDeed(14) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(15) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(16)) {
								this.boardFrame.getBoard().getContainer(26).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(26).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(27).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(27).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(29).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(29).getEstate().setBuildable(true);
						}
					}
					
					if( 31==position || 32==position || 34==position ) {
						if(31==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(17);
						if(32==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(18);
						if(34==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(19);
						
						if(null!=this.boardFrame.getCurrentPlayer().getDeed(17) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(18) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(19)) {
								this.boardFrame.getBoard().getContainer(31).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(31).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(32).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(32).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(34).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(34).getEstate().setBuildable(true);
						}
					}
					
					if( 37==position || 39==position ) {
						if(37==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(20);
						if(39==position)
							this.boardFrame.getCurrentPlayer().acquireDeed(21);
						
						if(null!=this.boardFrame.getCurrentPlayer().getDeed(20) &&
						   null!=this.boardFrame.getCurrentPlayer().getDeed(21)) {
								this.boardFrame.getBoard().getContainer(37).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(37).getEstate().setBuildable(true);
								this.boardFrame.getBoard().getContainer(39).getEstate().setMonopoly(true);
								this.boardFrame.getBoard().getContainer(39).getEstate().setBuildable(true);
						}
					}
			}
		}
		
		return transactionDone;
	}
	
	
	//EVENT METHODS
	//Selected BUY Button
	private void buyButtonActionActionPerformed(ActionEvent event) {

		//Setup ownership of current property
		boolean transactionDone = this.setupOwnership();
		this.boardFrame.setupMarker(this.boardFrame.getCurrentPlayer().getToken(),this.boardFrame.getCurrentPlayer().getToken().getPosition());
		this.boardFrame.resetMenuButtons();
		if(transactionDone)
			this.dispose();
	}
	
	private void auctionButtonActionActionPerformed(ActionEvent event) {
		AuctionDialog auction = new AuctionDialog(this.boardFrame);
		auction.setVisible(true);
		this.dispose();
		
	}

}
